package com.fbinggun.fbinggunutil;

import com.fbinggun.fbinggunutil.API.ConfigUtil;
import com.fbinggun.fbinggunutil.API.PluginUtil;
import com.fbinggun.fbinggunutil.API.SqlUtil;
import com.fbinggun.fbinggunutil.fgive.Fgive;
import com.fbinggun.fbinggunutil.fgive.FgiveAPI;
import com.fbinggun.fbinggunutil.statistics.*;
import com.fbinggun.sql.SQLite;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;
import com.fbinggun.fbinggunutil.gui.GuiListener;

import java.io.File;
import java.sql.SQLException;

public final class FBinggunUtil extends FPlugin {
    public static FBinggunUtil plugin;
    private  GuiListener guiListener;
    private  FileConfiguration itemLang;
    private   SQLite sql;
    private StatistMain statistMain;
    private FileConfiguration language;
    private Fgive fgive;
    public String getSqlPrefix(){
        return getConfig().getString("SQL.table");
    }
    //菜单监听器管理
    public GuiListener getGuiListener() {
        return guiListener;
    }

    public FileConfiguration getLanguage() {
        return language;
    }

    //物品汉化类
    public FileConfiguration getItemLang() {
        return itemLang;
    }
    //主数据库类
    public SQLite getSql() {
        return sql;
    }

    @Override
    public void onEnable() {
        reload();
    }
    @Override
    public void reload() {
        plugin=this;
        //加载配置文件
        saveDefaultConfig();
        reloadConfig();
        itemLang=  ConfigUtil.load(this,new File(getDataFolder().getPath(),"ItemLang.yml"));
        language=ConfigUtil.load(this,new File(getDataFolder().getPath(),"Lang.yml"));
        try {
            sql= SqlUtil.reloadSQL(this,getConfig().getConfigurationSection("SQL"));
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        //启动fgive物品管理模块
        fgive=new Fgive();
        fgive.onEnable();

        //注册菜单管监听器
        guiListener= new GuiListener();
        getServer().getPluginManager().registerEvents(guiListener,this);
        PluginUtil.setupEconomy();

        // 启动模块
        if(isModuleEnable("Statist")){
            statistMain= new StatistMain();
            statistMain.onEnable();
            this.plugin. getLogger().info("Statist模块启动");
        }


    }

    public FgiveAPI getFgiveAPI() {
        return fgive.getApi();
    }

    public Fgive getFgive() {
        return fgive;
    }

    public StatistMain getStatistMain() {
        if(isModuleEnable("Statist")){
            return statistMain;
        }
        getLogger().warning("Statist模块未启动");
        return null;
    }

    //判断模块是否启动
    public boolean isModuleEnable(String module) {
        return getConfig().getBoolean("Module."+module);
    }
    //获得物品汉化名
    public  String getItemLang(String key){
        if(plugin.itemLang!=null){
            String lang =plugin.itemLang.getString(key,key);
            return lang;
        }
        return key;
    }
    //获得物品汉化名
    public  String getItemLang(ItemStack itemStack){
        if(itemStack!=null) {
            String name = getItemLang(itemStack.getType().name().toUpperCase());
            if(name==null){
                return itemStack.getType().name();
            }
            return name;
        }
        return "";
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }



    @Override
    public FPlugin getPlugin() {
        return plugin;
    }
}
